package com.zp.self.module.令牌桶.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.zp.self.module.令牌桶.service.ISysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/3/30 21:50
 * @Modified By
 */
@RestController
public class SysUserController {
    @Resource
    ISysUserService iSysUserService;

    // Guava令牌桶：每秒放行10个请求
    RateLimiter rateLimiter = RateLimiter.create(10);
    /**
     * 根据用户编号获取详细信息
     * 测试结果：单服务用缓存：1W的并发完全扛得住
     * 100请求    --95%的请求<3ms
     * 10000请求    --95%的请求<13ms
     */
    @GetMapping(value = "/{userId}")
    public Object getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        return iSysUserService.getById(userId);
    }
    /**
     * 根据用户编号获取详细信息
     * 测试结果：单服务用缓存：1W的并发完全扛得住
     * 1w请求    --95%的请求<3ms
     */
    @GetMapping(value = "cache/{userId}")
    public Object cacheId(@PathVariable(value = "userId", required = false) Long userId) {
//        if(!rateLimiter.tryAcquire())
//            return "真遗憾你手速满了...";
        //LockSupport.parkNanos(200*1000000L);//休眠200毫秒
        return iSysUserService.findById(userId);
    }
}
